(function($){
    
    $.fn.geomap = function(options, place){
           
        function setPlace(map, place) {
            
            if (place.geometry && place.geometry.viewport) {
                
                var viewport = new google.maps.LatLngBounds(
                    new google.maps.LatLng(
                            place.geometry.viewport.southwest.lat,
                            place.geometry.viewport.southwest.lng
                    ), 
                    new  google.maps.LatLng(
                            place.geometry.viewport.northeast.lat,
                            place.geometry.viewport.northeast.lng
                    )
                );
                map.fitBounds(viewport);
                return true;
                
            } else if (place.geometry && place.geometry.location) {
                
                var location = new google.maps.LatLng(
                    place.geometry.location.lat,
                    place.geometry.location.lng
                );
                map.setCenter(location);
                map.setZoom(17);  // Why 17? Because it looks good.
                return true;
                
            }
            return false;
        }

        function addMarker(map, place) {
            var location = new google.maps.LatLng(
                place.geometry.location.lat,
                place.geometry.location.lng
            );
            var marker = new google.maps.Marker({
                position: location,
                map: map,
                title: 'Hello World!'
            });
        }
        
        if (typeof options == "string") {
            
            var $element = $(this[0]);
            var data = $element.data("geomap");

            switch(options) {
                case "setPlace":
                    if (place) {
                        return setPlace(data.map, place);
                    }
                    return false;
                case "addMarker":
                    if (place) {
                        return addMarker(data.map, place);
                    }
                    return false;
            }
        } else {    
            
            
            options = $.extend({
                center: new google.maps.LatLng(-33.8688, 151.2195),
                zoom: 13,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                disableDefaultUI: true
            }, options);

            var make = function(){
                var $this = $(this);
                var map = new google.maps.Map(this, options);
                var data = {
                    map: map
                };
                $this.data("geomap", data);

            };

    return this.each(make);
    
    }

};
})(jQuery);